package com.woniu.controller;



import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.controller.form.ChangeFatherNowForm;
import com.woniu.controller.form.ChangeStatusForm;
import com.woniu.controller.form.FindUseFulTypeForm;
import com.woniu.controller.form.pageForm;
import com.woniu.model.Result;
import com.woniu.dto.AddGoodsTypeDto;
import com.woniu.form.AddGoodsTypeForm;
import com.woniu.model.GoodsType;
import com.woniu.params.*;
import com.woniu.service.GoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月25日
 */
@RestController
@Validated
@RequestMapping("/goodsType")
@Api(tags = "商品类型接口")
public class GoodsTypeController {
    @Autowired
    private GoodsTypeService goodsTypeService;
//    @PostMapping("/addGoodsType")
//    @ApiOperation("新增积分商品的类型(一次可新增多个)")
//    public Result<List<AddGoodsTypeDto>> addGoodsType(List<AddGoodsTypeForm> form){
//        List<AddGoodsTypeDto> dtos =  goodsTypeService.addGoodsType(BeanUtil.copyToList(form, AddGoodsTypeParam.class));
//        return Result.newListInstance(AddGoodsTypeDto.class).setData(dtos);
//    }

    /**
     * 新增积分商品类型
     * @param form
     * @return
     */
    @PostMapping("/addGoodsType.api")
    @ApiOperation("新增积分商品类型")
    public Result<GoodsType> addGoodsType(@RequestBody @Validated AddGoodsTypeForm form){
        GoodsType goodsType =  goodsTypeService.addGoodsType(BeanUtil.toBean(form,AddGoodsTypeParam.class));
        return Result.newInstance(GoodsType.class).setCode(200).setData(goodsType);
    }

    /**
     * 禁用某个商品类别
     * @param typeId 商品类别id
     * @return
     * @throws Exception
     */
    @GetMapping("/disable.api")
    @ApiOperation("禁用某个商品类别")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "username",value = "用户名",defaultValue = "hedon",paramType = "query",dataType="String",required = true),
//            @ApiImplicitParam(name = "password",value = "密码",defaultValue = "hedonpassword",paramType = "query",dataType="String",required = true)
//    })
    public Result<List<GoodsType>> disable(@ApiParam(name = "typeId",value = "商品类型id") @NotNull(message = "类型id不能为空") @RequestParam("typeId") Integer typeId) throws Exception {
//        System.out.println("我是控制层的数据！！！！！！！！！！！！！");
//        System.out.println(typeId);
//        System.out.println(typeState);
        List<GoodsType> goodsTypes = goodsTypeService.disable(typeId);
        return Result.newListInstance(GoodsType.class).setCode(200).setData(goodsTypes).setMessage("禁用图书成功");
    }

    /**
     * 禁用某个商品类别
     * @param form 改变状态form
     * @return null
     */
    @PostMapping("/changeStatus.api")
    @ApiOperation("改变商品类别状态")
    public Result<?> disable(@RequestBody ChangeStatusForm form) throws Exception {
        goodsTypeService.changeStatus(BeanUtil.toBean(form, ChangeStatusParam.class));
        return Result.newInstance(null).setCode(200).setMessage("改变商品类别状态成功");
    }


    /**
     * 启用某个商品类别
     * @param typeId 商品类别id
     * @return
     * @throws Exception
     */
    @GetMapping("/enable.api")
    @ApiOperation("启用某个商品类别")
    public Result<String> enable(@ApiParam(name = "typeId",value = "商品类型id") @NotNull(message = "类型id不能为空") @RequestParam("typeId") Integer typeId) throws Exception {
//        System.out.println("我是控制层的数据！！！！！！！！！");
//        System.out.println(parentId);
        goodsTypeService.enable(typeId);
        return Result.newInstance(String.class).setCode(200).setMessage("启用图书成功");
    }

    /**
     * 修改商品类型的父类别
     * @param parentId 父类别id
     * @param typeId 商品类别id
     * @return
     * @throws Exception
     */
    @GetMapping("/modify.api")
    @ApiOperation("修改商品类型的父类别")
    public Result<String> modify(@ApiParam(name = "parentId",value = "被修改的父类别Id(0表示最高夫类别)")
                                     @NotNull(message = "类型id不能为空")
                                     @PositiveOrZero(message = "类型id必须是正整数")
                                     @RequestParam("parentId")
                                     Integer parentId,
                                     @ApiParam(name = "typeId",value = "商品类型id")
                                     @RequestParam("typeId")
                                     Integer typeId) throws Exception {
        goodsTypeService.modify(parentId,typeId);
        return Result.newInstance(String.class).setCode(200).setMessage("修改图书类别成功");
    }

    /**
     * 查询所有商品类别Page
     * @param form page分页form
     * @return page
     */
    @PostMapping("/getAll")
    @ApiOperation("查询所有商品类别Page")
    public Result<Page<GoodsType>> getAll(@RequestBody pageForm form) throws Exception {
        Page<GoodsType> page = goodsTypeService.getAll(BeanUtil.toBean(form, pageParam.class));
        return Result.newPageInstance(GoodsType.class).setCode(200).setData(page).setMessage("查询所有商品类别成功");
    }
    /**
     * 查询所有商品类别list
     * @return page
     */
    @GetMapping("/getAllList")
    @ApiOperation("查询所有商品类别List")
    public Result<List<GoodsType>> getAllList() throws Exception {
        List<GoodsType> list = goodsTypeService.getAllList();
        return Result.newListInstance(GoodsType.class).setCode(200).setData(list).setMessage("查询所有商品类别成功");
    }

    /**
     * 查询可用的商品类别list
     * @return list
     */
    @PostMapping("/findUseFulType")
    @ApiOperation("查询可用的商品类别list")
    public Result<List<GoodsType>> findUseFulType(@RequestBody FindUseFulTypeForm form){
        List<GoodsType> list = goodsTypeService.findUseFulType(BeanUtil.toBean(form, FindUseFulTypeParam.class));
        return Result.newListInstance(GoodsType.class).setCode(200).setData(list).setMessage("查询所有商品类别成功");
    }

    /**
     * 更改商品父类
     * @param form 更改父类form
     * @return null
     */
    @PostMapping("/changeFatherNow")
    @ApiOperation("更改商品父类")
    public Result<?> changeFatherNow(@RequestBody ChangeFatherNowForm form){
        goodsTypeService.changeFatherNow(BeanUtil.toBean(form, ChangeFatherNowParam.class));
        return Result.newInstance(null).setCode(200).setMessage("更改商品父类成功");
    }
    /**
     * 删除商品类型
     * @param typeId 类别id
     * @return null
     */
    @GetMapping("/delType")
    @ApiOperation("删除商品类别")
    public Result delType(Integer typeId){
        goodsTypeService.delType(typeId);
        return Result.newInstance(null).setCode(200).setMessage("删除商品类别成功");
    }
}

